<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的辅助函数列表" tabindex="-1"><a class="header-anchor" href="#laravel-的辅助函数列表"><span>Laravel 的辅助函数列表</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#available-methods">可用方法</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 包含各种各样的全局「辅助」PHP 函数，框架本身也大量地使用了这些功能；如果你觉得方便，你可以在你的应用中自由的使用它们。</p>
<h2 id="可用方法" tabindex="-1"><a class="header-anchor" href="#可用方法"><span>可用方法</span></a></h2>
<h3 id="数组-对象" tabindex="-1"><a class="header-anchor" href="#数组-对象"><span>数组 &amp; 对象</span></a></h3>
<p><a href="#method-array-add">array_add</a><br>
<a href="#method-array-collapse">array_collapse</a><br>
<a href="#method-array-divide">array_divide</a><br>
<a href="#method-array-dot">array_dot</a><br>
<a href="#method-array-except">array_except</a><br>
<a href="#method-array-first">array_first</a><br>
<a href="#method-array-flatten">array_flatten</a><br>
<a href="#method-array-forget">array_forget</a><br>
<a href="#method-array-get">array_get</a><br>
<a href="#method-array-has">array_has</a><br>
<a href="#method-array-last">array_last</a><br>
<a href="#method-array-only">array_only</a><br>
<a href="#method-array-pluck">array_pluck</a><br>
<a href="#method-array-prepend">array_prepend</a><br>
<a href="#method-array-pull">array_pull</a><br>
<a href="#method-array-random">array_random</a><br>
<a href="#method-array-set">array_set</a><br>
<a href="#method-array-sort">array_sort</a><br>
<a href="#method-array-sort-recursive">array_sort_recursive</a><br>
<a href="#method-array-where">array_where</a><br>
<a href="#method-array-wrap">array_wrap</a><br>
<a href="#method-data-fill">data_fill</a><br>
<a href="#method-data-get">data_get</a><br>
<a href="#method-data-set">data_set</a><br>
<a href="#method-head">head</a><br>
<a href="#method-last">last</a></p>
<h3 id="路径" tabindex="-1"><a class="header-anchor" href="#路径"><span>路径</span></a></h3>
<p><a href="#method-app-path">app_path</a><br>
<a href="#method-base-path">base_path</a><br>
<a href="#method-config-path">config_path</a><br>
<a href="#method-database-path">database_path</a><br>
<a href="#method-mix">mix</a><br>
<a href="#method-public-path">public_path</a><br>
<a href="#method-resource-path">resource_path</a><br>
<a href="#method-storage-path">storage_path</a></p>
<h3 id="字符串" tabindex="-1"><a class="header-anchor" href="#字符串"><span>字符串</span></a></h3>
<p><a href="#method-__">__</a><br>
<a href="#method-camel-case">camel_case</a><br>
<a href="#method-class-basename">class_basename</a><br>
<a href="#method-e">e</a><br>
<a href="#method-ends-with">ends_with</a><br>
<a href="#method-kebab-case">kebab_case</a><br>
<a href="#method-preg-replace-array">preg_replace_array</a><br>
<a href="#method-snake-case">snake_case</a><br>
<a href="#method-starts-with">starts_with</a><br>
<a href="#method-str-after">str_after</a><br>
<a href="#method-str-before">str_before</a><br>
<a href="#method-str-contains">str_contains</a><br>
<a href="#method-str-finish">str_finish</a><br>
<a href="#method-str-is">str_is</a><br>
<a href="#method-str-limit">str_limit</a><br>
<a href="#method-str-plural">str_plural</a><br>
<a href="#method-str-random">str_random</a><br>
<a href="#method-str-replace-array">str_replace_array</a><br>
<a href="#method-str-replace-first">str_replace_first</a><br>
<a href="#method-str-replace-last">str_replace_last</a><br>
<a href="#method-str-singular">str_singular</a><br>
<a href="#method-str-slug">str_slug</a><br>
<a href="#method-str-start">str_start</a><br>
<a href="#method-studly-case">studly_case</a><br>
<a href="#method-title-case">title_case</a><br>
<a href="#method-trans">trans</a><br>
<a href="#method-trans-choice">trans_choice</a></p>
<h3 id="urls" tabindex="-1"><a class="header-anchor" href="#urls"><span>URLs</span></a></h3>
<p><a href="#method-action">action</a><br>
<a href="#method-asset">asset</a><br>
<a href="#method-secure-asset">secure_asset</a><br>
<a href="#method-route">route</a><br>
<a href="#method-secure-url">secure_url</a><br>
<a href="#method-url">url</a></p>
<h3 id="其他" tabindex="-1"><a class="header-anchor" href="#其他"><span>其他</span></a></h3>
<p><a href="#method-abort">abort</a><br>
<a href="#method-abort-if">abort_if</a><br>
<a href="#method-abort-unless">abort_unless</a><br>
<a href="#method-app">app</a><br>
<a href="#method-auth">auth</a><br>
<a href="#method-back">back</a><br>
<a href="#method-bcrypt">bcrypt</a><br>
<a href="#method-blank">blank</a><br>
<a href="#method-broadcast">broadcast</a><br>
<a href="#method-cache">cache</a><br>
<a href="#method-class-uses-recursive">class_uses_recursive</a><br>
<a href="#method-collect">collect</a><br>
<a href="#method-config">config</a><br>
<a href="#method-cookie">cookie</a><br>
<a href="#method-csrf-field">csrf_field</a><br>
<a href="#method-csrf-token">csrf_token</a><br>
<a href="#method-dd">dd</a><br>
<a href="#method-decrypt">decrypt</a><br>
<a href="#method-dispatch">dispatch</a><br>
<a href="#method-dispatch-now">dispatch_now</a><br>
<a href="#method-dump">dump</a><br>
<a href="#method-encrypt">encrypt</a><br>
<a href="#method-env">env</a><br>
<a href="#method-event">event</a><br>
<a href="#method-factory">factory</a><br>
<a href="#method-filled">filled</a><br>
<a href="#method-info">info</a><br>
<a href="#method-logger">logger</a><br>
<a href="#method-method-field">method_field</a><br>
<a href="#method-now">now</a><br>
<a href="#method-old">old</a><br>
<a href="#method-optional">optional</a><br>
<a href="#method-policy">policy</a><br>
<a href="#method-redirect">redirect</a><br>
<a href="#method-report">report</a><br>
<a href="#method-request">request</a><br>
<a href="#method-rescue">rescue</a><br>
<a href="#method-resolve">resolve</a><br>
<a href="#method-response">response</a><br>
<a href="#method-retry">retry</a><br>
<a href="#method-session">session</a><br>
<a href="#method-tap">tap</a><br>
<a href="#method-today">today</a><br>
<a href="#method-throw-if">throw_if</a><br>
<a href="#method-throw-unless">throw_unless</a><br>
<a href="#method-trait-uses-recursive">trait_uses_recursive</a><br>
<a href="#method-transform">transform</a><br>
<a href="#method-validator">validator</a><br>
<a href="#method-value">value</a><br>
<a href="#method-view">view</a><br>
<a href="#method-with">with</a></p>
<h2 id="方法列表" tabindex="-1"><a class="header-anchor" href="#方法列表"><span>方法列表</span></a></h2>
<h2 id="数组" tabindex="-1"><a class="header-anchor" href="#数组"><span>数组</span></a></h2>
<h4 id="array-add" tabindex="-1"><a class="header-anchor" href="#array-add"><span><code v-pre>array_add()</code></span></a></h4>
<p>如果给定的键不在数组中，那么 <code v-pre>array_add</code> 函数将会把给定的键/值对添加到数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-collapse" tabindex="-1"><a class="header-anchor" href="#array-collapse"><span><code v-pre>array_collapse()</code></span></a></h4>
<p><code v-pre>array_collapse</code> 函数将多个单数组合并成一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_collapse</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-divide" tabindex="-1"><a class="header-anchor" href="#array-divide"><span><code v-pre>array_divide()</code></span></a></h4>
<p><code v-pre>array_divide</code> 函数返回两个数组，一个包含原始数组的健，另一个包含原始数组的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">list</span><span class="token punctuation">(</span><span class="token variable">$keys</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token function">array_divide</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $keys: ['name']</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $values: ['Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-dot" tabindex="-1"><a class="header-anchor" href="#array-dot"><span><code v-pre>array_dot()</code></span></a></h4>
<p><code v-pre>array_dot</code> 函数将多维数组平铺到一维数组中，该数组使用「点」符号表示深度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_dot</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'bar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'baz'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['foo.bar' => 'baz'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-except" tabindex="-1"><a class="header-anchor" href="#array-except"><span><code v-pre>array_except()</code></span></a></h4>
<p><code v-pre>array_except</code> 函数从数组中删除给定的键/值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_except</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-first" tabindex="-1"><a class="header-anchor" href="#array-first"><span><code v-pre>array_first()</code></span></a></h4>
<p><code v-pre>array_first</code> 函数返回数组中第一个通过指定测试的元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 200</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法。如果没有值通过测试，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array-flatten" tabindex="-1"><a class="header-anchor" href="#array-flatten"><span><code v-pre>array_flatten()</code></span></a></h4>
<p><code v-pre>array_flatten</code> 函数将多维数组平铺为一维数组。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Joe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'languages'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_flatten</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Joe', 'PHP', 'Ruby'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-forget" tabindex="-1"><a class="header-anchor" href="#array-forget"><span><code v-pre>array_forget()</code></span></a></h4>
<p><code v-pre>array_forget</code> 函数使用「点」符号从深度嵌套数组中移除给定的键/值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">array_forget</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => []]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-get" tabindex="-1"><a class="header-anchor" href="#array-get"><span><code v-pre>array_get()</code></span></a></h4>
<p><code v-pre>array_get</code> 函数使用「点」符号从深度嵌套的数组中检索值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>array_get</code> 函数也接受一个默认值，如果没有找到指定的健，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'names.john'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array-has" tabindex="-1"><a class="header-anchor" href="#array-has"><span><code v-pre>array_has()</code></span></a></h4>
<p><code v-pre>array_has</code> 函数使用「点」符号检查数组中是否存在给定的项目或项目组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$hasItem</span> <span class="token operator">=</span> <span class="token function">array_has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$hasItems</span> <span class="token operator">=</span> <span class="token function">array_has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product.price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-last" tabindex="-1"><a class="header-anchor" href="#array-last"><span><code v-pre>array_last()</code></span></a></h4>
<p><code v-pre>array_last</code> 函数返回数组中最后一个通过指定测试的元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法。如果没有值通过测试，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token function">array_last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array-only" tabindex="-1"><a class="header-anchor" href="#array-only"><span><code v-pre>array_only()</code></span></a></h4>
<p><code v-pre>array_only</code> 函数仅返回给定数组中指定的键/值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_only</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-pluck" tabindex="-1"><a class="header-anchor" href="#array-pluck"><span><code v-pre>array_pluck()</code></span></a></h4>
<p><code v-pre>array_pluck</code> 函数从数组中检索给定键的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Taylor', 'Abigail'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定生成的列表的键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => 'Taylor', 2 => 'Abigail'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-prepend" tabindex="-1"><a class="header-anchor" href="#array-prepend"><span><code v-pre>array_prepend()</code></span></a></h4>
<p><code v-pre>array_prepend</code> 函数将一个项目推到数组的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'one'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'two'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'three'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'four'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'zero'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $array: ['zero', 'one', 'two', 'three', 'four']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以指定用于该值的键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-pull" tabindex="-1"><a class="header-anchor" href="#array-pull"><span><code v-pre>array_pull()</code></span></a></h4>
<p><code v-pre>array_pull</code> 函数返回并从数组中删除键/值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token function">array_pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $name: Desk</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $array: ['price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法。如果键不存在，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">array_pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array-random" tabindex="-1"><a class="header-anchor" href="#array-random"><span><code v-pre>array_random()</code></span></a></h4>
<p><code v-pre>array_random</code> 函数从数组中返回一个随机值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$random</span> <span class="token operator">=</span> <span class="token function">array_random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 4 - (随机获取)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定要返回的随机数的数量作为第二个可选参数。一旦你指定了第二个参数，即使数量为 1，这个函数也会返回一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$items</span> <span class="token operator">=</span> <span class="token function">array_random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [2, 5] - (随机获取)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-set" tabindex="-1"><a class="header-anchor" href="#array-set"><span><code v-pre>array_set()</code></span></a></h4>
<p><code v-pre>array_set</code> 函数使用「点」符号在深度嵌套的数组中设置一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">array_set</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-sort" tabindex="-1"><a class="header-anchor" href="#array-sort"><span><code v-pre>array_sort()</code></span></a></h4>
<p><code v-pre>array_sort</code> 函数按照其值排序数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Chair', 'Desk', 'Table']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以按给定的闭包返回的结果对数组进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_values</span><span class="token punctuation">(</span><span class="token function">array_sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['name' => 'Chair'],</span>
<span class="line">        ['name' => 'Desk'],</span>
<span class="line">        ['name' => 'Table'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-sort-recursive" tabindex="-1"><a class="header-anchor" href="#array-sort-recursive"><span><code v-pre>array_sort_recursive()</code></span></a></h4>
<p><code v-pre>array_sort_recursive</code> 函数使用 <code v-pre>sort</code> 函数递归排序数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Roman'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Li'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'JavaScript'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_sort_recursive</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['Li', 'Roman', 'Taylor'],</span>
<span class="line">        ['JavaScript', 'PHP', 'Ruby'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-where" tabindex="-1"><a class="header-anchor" href="#array-where"><span><code v-pre>array_where()</code></span></a></h4>
<p><code v-pre>array_where</code> 函数使用给定的闭包来过滤数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'200'</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'400'</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_where</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">is_string</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => 200, 3 => 400]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="array-wrap" tabindex="-1"><a class="header-anchor" href="#array-wrap"><span><code v-pre>array_wrap()</code></span></a></h4>
<p><code v-pre>array_wrap</code> 函数将给定的值包装成一个数组。如果给定的值已经是一个数组，则不会被改变：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token function">array_wrap</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [0 => 'Laravel']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-fill" tabindex="-1"><a class="header-anchor" href="#data-fill"><span><code v-pre>data_fill()</code></span></a></h4>
<p><code v-pre>data_fill</code> 函数使用「点」符号在嵌套数组或对象内设置缺少的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该函数也接受星号「*」作为通配符，并相应地填写目标：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1', 'price' => 100],</span>
<span class="line">            ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-get" tabindex="-1"><a class="header-anchor" href="#data-get"><span><code v-pre>data_get()</code></span></a></h4>
<p><code v-pre>data_get</code> 函数使用「点」符号从嵌套数组或对象中检索值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>data_get</code> 函数还接受默认值作为第三个参数，如果找不到指定的键，将返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$discount</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-set" tabindex="-1"><a class="header-anchor" href="#data-set"><span><code v-pre>data_set()</code></span></a></h4>
<p><code v-pre>data_set</code> 函数使用「点」符号在嵌套数组或对象内设置一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这个函数也接受通配符「*」，并相应地在目标上设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">/*</span>
<span class="line">        [</span>
<span class="line">            'products' => [</span>
<span class="line">                ['name' => 'Desk 1', 'price' => 200],</span>
<span class="line">                ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">            ],</span>
<span class="line">        ]</span>
<span class="line">    */</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，所有现有的值都会被覆盖。如果你只想设置一个不存在值，你可以传递 <code v-pre>false</code> 作为第三个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="head" tabindex="-1"><a class="header-anchor" href="#head"><span><code v-pre>head()</code></span></a></h4>
<p><code v-pre>head</code> 函数返回给定数组中的第一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token function">head</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="last" tabindex="-1"><a class="header-anchor" href="#last"><span><code v-pre>last()</code></span></a></h4>
<p><code v-pre>last</code> 函数返回给定数组中的最后一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路径-1" tabindex="-1"><a class="header-anchor" href="#路径-1"><span>路径</span></a></h2>
<h4 id="app-path" tabindex="-1"><a class="header-anchor" href="#app-path"><span><code v-pre>app_path()</code></span></a></h4>
<p><code v-pre>app_path</code> 返回 <code v-pre>app</code> 目录的完整路径。你还可以使用 <code v-pre>app_path</code> 函数来生成相对于 <code v-pre>app</code> 目录的文件完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Http/Controllers/Controller.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="base-path" tabindex="-1"><a class="header-anchor" href="#base-path"><span><code v-pre>base_path()</code></span></a></h4>
<p><code v-pre>base_path</code> 函数返回项目根目录的完整路径。你还可以使用 <code v-pre>base_path</code> 函数生成指定文件相对于项目根目录的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/bin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="config-path" tabindex="-1"><a class="header-anchor" href="#config-path"><span><code v-pre>config_path()</code></span></a></h4>
<p><code v-pre>config_path</code> 函数返回应用程序配置目录的完整路径。你也可以使用 <code v-pre>config_path</code> 函数来生成应用程序配置目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="database-path" tabindex="-1"><a class="header-anchor" href="#database-path"><span><code v-pre>database_path()</code></span></a></h4>
<p><code v-pre>database_path</code> 函数返回应用程序数据库目录的完整路径。你也可以使用 <code v-pre>database_path</code> 函数来生成数据库目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="mix" tabindex="-1"><a class="header-anchor" href="#mix"><span><code v-pre>mix()</code></span></a></h4>
<p><code v-pre>mix</code> 函数获取 <a href="https://learnku.com/docs/laravel/5.5/mix" target="_blank" rel="noopener noreferrer">版本化 Mix 文件</a> 的路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">mix</span><span class="token punctuation">(</span><span class="token variable">$file</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="public-path" tabindex="-1"><a class="header-anchor" href="#public-path"><span><code v-pre>public_path()</code></span></a></h4>
<p><code v-pre>public_path</code> 函数返回 <code v-pre>public</code> 目录的完整路径。你也可以使用 <code v-pre>public_path</code> 函数来生成 <code v-pre>public</code> 目录中给定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="resource-path" tabindex="-1"><a class="header-anchor" href="#resource-path"><span><code v-pre>resource_path()</code></span></a></h4>
<p><code v-pre>resource_path</code> 函数返回 <code v-pre>resources</code> 目录的完整路径。你也可以使用 <code v-pre>resource_path</code> 函数来生成相对于资源目录的指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'assets/sass/app.scss'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="storage-path" tabindex="-1"><a class="header-anchor" href="#storage-path"><span><code v-pre>storage_path()</code></span></a></h4>
<p><code v-pre>storage_path</code> 函数返回 <code v-pre>storage</code> 目录的完整路径。你也可以使用 <code v-pre>storage_path</code> 来生成相对于储存目录的指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="字符串-1" tabindex="-1"><a class="header-anchor" href="#字符串-1"><span>字符串</span></a></h2>
<h4 id="" tabindex="-1"><a class="header-anchor" href="#"><span><code v-pre>__()</code></span></a></h4>
<p><code v-pre>__</code> 函数使用你的 <a href="https://learnku.com/docs/laravel/5.5/localization" target="_blank" rel="noopener noreferrer">本地化文件</a> 来翻译给定的翻译字符串或翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Welcome to our application'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果指定的翻译字符串或键不存在，则 <code v-pre>__</code> 函数会简单地返回给定的值。所以，按照上面的例子，如果翻译键 <code v-pre>messages.welcome</code> 不存在，<code v-pre>__</code> 方法会将其直接返回。</p>
<h4 id="camel-case" tabindex="-1"><a class="header-anchor" href="#camel-case"><span><code v-pre>camel_case()</code></span></a></h4>
<p><code v-pre>camel_case</code> 函数将给定的值符传转换为「驼峰命名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$camel</span> <span class="token operator">=</span> <span class="token function">camel_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// fooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="class-basename" tabindex="-1"><a class="header-anchor" href="#class-basename"><span><code v-pre>class_basename()</code></span></a></h4>
<p><code v-pre>class_basename</code> 返回给定类删除命名空间的类名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$class</span> <span class="token operator">=</span> <span class="token function">class_basename</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Baz</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="e" tabindex="-1"><a class="header-anchor" href="#e"><span><code v-pre>e()</code></span></a></h4>
<p><code v-pre>e</code> 函数将 <code v-pre>double_encode</code> 选项设置为 <code v-pre>false</code> 来运行 PHP 的 <code v-pre>htmlspecialchars</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">e</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;html>foo&lt;/html>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;html>foo&lt;/html></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ends-with" tabindex="-1"><a class="header-anchor" href="#ends-with"><span><code v-pre>ends_with()</code></span></a></h4>
<p><code v-pre>ends_with</code> 函数判断给定的字符串是否以给定的值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">ends_with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="kebab-case" tabindex="-1"><a class="header-anchor" href="#kebab-case"><span><code v-pre>kebab_case()</code></span></a></h4>
<p><code v-pre>kebab_case</code> 函数将给定的字符串转换为「短横线命名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">kebab_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="preg-replace-array" tabindex="-1"><a class="header-anchor" href="#preg-replace-array"><span><code v-pre>preg_replace_array()</code></span></a></h4>
<p><code v-pre>preg_replace_array</code> 函数使用数组顺序替换字符串中的给定模式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'活动将在 :start 和 :end 之间进行'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">preg_replace_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/:[a-z_]+/'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 活动将在 8:30 至 9:00 之间进行</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="snake-case" tabindex="-1"><a class="header-anchor" href="#snake-case"><span><code v-pre>snake_case()</code></span></a></h4>
<p><code v-pre>snake_case</code> 函数将给定的字符串转换为「蛇形命名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$snake</span> <span class="token operator">=</span> <span class="token function">snake_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo_bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="starts-with" tabindex="-1"><a class="header-anchor" href="#starts-with"><span><code v-pre>starts_with()</code></span></a></h4>
<p><code v-pre>starts_with</code> 函数判断给定的字符串的开头是否是指定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">starts_with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-after" tabindex="-1"><a class="header-anchor" href="#str-after"><span><code v-pre>str_after()</code></span></a></h4>
<p><code v-pre>str_after</code> 函数返回字符串中指定值之后的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token function">str_after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-before" tabindex="-1"><a class="header-anchor" href="#str-before"><span><code v-pre>str_before()</code></span></a></h4>
<p><code v-pre>str_before</code> 函数返回字符串中给定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token function">str_before</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This is '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-contains" tabindex="-1"><a class="header-anchor" href="#str-contains"><span><code v-pre>str_contains()</code></span></a></h4>
<p><code v-pre>str_contains</code> 函数判断给定的字符串是否包含给定的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递一个值的数组来判断给定的字符串是否包含任何值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-finish" tabindex="-1"><a class="header-anchor" href="#str-finish"><span><code v-pre>str_finish()</code></span></a></h4>
<p><code v-pre>str_finish</code> 函数将给定字符串以给定值结尾返回（如果它尚未以给定值结尾）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str_finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string2</span> <span class="token operator">=</span> <span class="token function">str_finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-is" tabindex="-1"><a class="header-anchor" href="#str-is"><span><code v-pre>str_is()</code></span></a></h4>
<p><code v-pre>str_is</code> 函数判断给定的字符串是否匹配给定的模式。星号可以用来表示通配符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">str_is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'baz*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-limit" tabindex="-1"><a class="header-anchor" href="#str-limit"><span><code v-pre>str_limit()</code></span></a></h4>
<p><code v-pre>str_limit</code> 函数按给定的长度截断给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token function">str_limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递第三个参数来改变将被追加到最后的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token function">str_limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' (...)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox (...)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-plural" tabindex="-1"><a class="header-anchor" href="#str-plural"><span><code v-pre>str_plural()</code></span></a></h4>
<p><code v-pre>str_plural</code> 函数将字符串转换为复数形式。这个函数目前仅支持英文：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// cars</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以提供一个整数作为函数的第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token function">str_plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-random" tabindex="-1"><a class="header-anchor" href="#str-random"><span><code v-pre>str_random()</code></span></a></h4>
<p><code v-pre>str_random</code> 函数生成一个指定长度的随机字符串。这个函数数用 PHP 的 <code v-pre>random_bytes</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str_random</span><span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="str-replace-array" tabindex="-1"><a class="header-anchor" href="#str-replace-array"><span><code v-pre>str_replace_array()</code></span></a></h4>
<p><code v-pre>str_replace_array</code> 函数使用数组顺序替换字符串中的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between ? and ?'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">str_replace_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replace-first" tabindex="-1"><a class="header-anchor" href="#str-replace-first"><span><code v-pre>str_replace_first()</code></span></a></h4>
<p><code v-pre>str_replace_first</code> 函数替换字符串中给定值的第一个匹配项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">str_replace_first</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// a quick brown fox jumps over the lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replace-last" tabindex="-1"><a class="header-anchor" href="#str-replace-last"><span><code v-pre>str_replace_last()</code></span></a></h4>
<p><code v-pre>str_replace_last</code> 函数替换字符串中最后一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">str_replace_last</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// the quick brown fox jumps over a lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-singular" tabindex="-1"><a class="header-anchor" href="#str-singular"><span><code v-pre>str_singular()</code></span></a></h4>
<p><code v-pre>str_singular</code> 函数将字符串转换为单数形式。这个函数目前仅支持英文：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token function">str_singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// car</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-slug" tabindex="-1"><a class="header-anchor" href="#str-slug"><span><code v-pre>str_slug()</code></span></a></h4>
<p><code v-pre>str_slug</code> 函数根据给定的字符串生成一个 URL 友好的「slug」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token function">str_slug</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel 5 Framework'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel-5-framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-start" tabindex="-1"><a class="header-anchor" href="#str-start"><span><code v-pre>str_start()</code></span></a></h4>
<p><code v-pre>str_start</code> 函数将给定值的单个实例添加到字符串（如果它尚未以值开始）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token function">str_start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token function">str_start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/this/string/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="studly-case" tabindex="-1"><a class="header-anchor" href="#studly-case"><span><code v-pre>studly_case()</code></span></a></h4>
<p><code v-pre>studly_case</code> 函数将给定的字符串转换为「变种驼峰命名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">studly_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// FooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="title-case" tabindex="-1"><a class="header-anchor" href="#title-case"><span><code v-pre>title_case()</code></span></a></h4>
<p><code v-pre>title_case</code> 函数将给定的字符串转换为「首字母大写」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token function">title_case</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// A Nice Title Uses The Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trans" tabindex="-1"><a class="header-anchor" href="#trans"><span><code v-pre>trans()</code></span></a></h4>
<p><code v-pre>trans</code> 函数使用你的 <a href="https://learnku.com/docs/laravel/5.5/localization" target="_blank" rel="noopener noreferrer">本地化文件</a> 来翻译给定的翻译字符串或翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，则 <code v-pre>trans</code> 方法会简单地返回给定的键。所以，就上面的例子而言，如果翻译键不存在， <code v-pre>trans</code> 方法会返回 <code v-pre>messages.welcome</code>。</p>
<h4 id="trans-choice" tabindex="-1"><a class="header-anchor" href="#trans-choice"><span><code v-pre>trans_choice()</code></span></a></h4>
<p><code v-pre>trans_choice</code> 函数根据词形变化来翻译给定的翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.notifications'</span><span class="token punctuation">,</span> <span class="token variable">$unreadCount</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，<code v-pre>trans_choice</code> 方法会简单地返回给定的键。所以，按照上面的例子，如果翻译键不存在，<code v-pre>trans_choice</code> 方法会返回 <code v-pre>messages.notifications</code>。</p>
<h2 id="urls-1" tabindex="-1"><a class="header-anchor" href="#urls-1"><span>URLs</span></a></h2>
<h4 id="action" tabindex="-1"><a class="header-anchor" href="#action"><span><code v-pre>action()</code></span></a></h4>
<p><code v-pre>action</code> 函数为指定的控制器动作生成一个 URL。你不需要传递完整的控制器命名空间。只需要传递相对于 <code v-pre>App\Http\Controllers</code> 的命名空间的控制器类名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HomeController@getIndex'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果该方法接受路由参数，则可以将它们作为方法的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserController@profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asset" tabindex="-1"><a class="header-anchor" href="#asset"><span><code v-pre>asset()</code></span></a></h4>
<p><code v-pre>asset</code> 函数使用当前请求的协议（ HTTP 或 HTTPS ）为资源文件生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-asset" tabindex="-1"><a class="header-anchor" href="#secure-asset"><span><code v-pre>secure_asset()</code></span></a></h4>
<p><code v-pre>secure_asset</code> 函数使用 HTTPS 协议为资源文件生成 URL:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">secure_asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar.zip'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="route" tabindex="-1"><a class="header-anchor" href="#route"><span><code v-pre>route()</code></span></a></h4>
<p><code v-pre>route</code> 函数为给定的命名路由生成一个 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'routeName'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果路由接受参数，则可以将它们作为方法的第二个参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'routeName'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>route</code> 函数生成的是绝对 URL。如果你想生成一个相对 URL，你可以传递 <code v-pre>false</code> 作为第三个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'routeName'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-url" tabindex="-1"><a class="header-anchor" href="#secure-url"><span><code v-pre>secure_url()</code></span></a></h4>
<p><code v-pre>secure_url</code> 函数为给定的路径生成一个标准的 HTTPS URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span><code v-pre>url()</code></span></a></h4>
<p><code v-pre>url</code> 函数生成给定路径的标准 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有提供路径，则返回 <code v-pre>Illuminate\Routing\UrlGenerator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">full</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">previous</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="其他-1" tabindex="-1"><a class="header-anchor" href="#其他-1"><span>其他</span></a></h2>
<h4 id="abort" tabindex="-1"><a class="header-anchor" href="#abort"><span><code v-pre>abort()</code></span></a></h4>
<p><code v-pre>abort</code> 函数抛出 <a href="https://learnku.com/docs/laravel/5.5/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理</a> 程序呈现的 <a href="https://learnku.com/docs/laravel/5.5/errors#http-exceptions" target="_blank" rel="noopener noreferrer">HTTP 异常</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">401</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以提供额外的响应文本和自定义响应标头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Unauthorized.'</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="abort-if" tabindex="-1"><a class="header-anchor" href="#abort-if"><span><code v-pre>abort_if()</code></span></a></h4>
<p>如果给定的布尔表达式计算结果为 <code v-pre>true</code>， <code v-pre>abort_if</code> 函数将抛出一个 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>和 <code v-pre>abort</code> 方法一样，你也可以提供异常的响应文本作为第三个参数，并提供一个自定义响应头数组作为第四个参数。</p>
<h4 id="abort-unless" tabindex="-1"><a class="header-anchor" href="#abort-unless"><span><code v-pre>abort_unless()</code></span></a></h4>
<p>如果给定的布尔表达式计算结果为 <code v-pre>false</code>，<code v-pre>abort_unless</code> 函数将抛出一个 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>和 <code v-pre>abort</code> 方法一样，你也可以提供异常的响应文本作为第三个参数，并提供一个自定义响应头数组作为第四个参数。</p>
<h4 id="app" tabindex="-1"><a class="header-anchor" href="#app"><span><code v-pre>app()</code></span></a></h4>
<p><code v-pre>app</code> 函数返回 <a href="https://learnku.com/docs/laravel/5.5/container" target="_blank" rel="noopener noreferrer">服务容器</a> 实例</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$container</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以传递一个类或接口名称来从容器中解析它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="auth" tabindex="-1"><a class="header-anchor" href="#auth"><span><code v-pre>auth()</code></span></a></h4>
<p><code v-pre>auth</code> 函数返回一个 <a href="https://learnku.com/docs/laravel/5.5/authentication" target="_blank" rel="noopener noreferrer">认证</a> 实例。为了方便起见，你可以使用它来替代 <code v-pre>Auth</code> facade：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要，你可以指定你想要访问的认证实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="back" tabindex="-1"><a class="header-anchor" href="#back"><span><code v-pre>back()</code></span></a></h4>
<p><code v-pre>back()</code> 函数生成一个 <a href="https://learnku.com/docs/laravel/5.5/responses#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a> 到用户之前的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token variable">$status</span> <span class="token operator">=</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$fallback</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="bcrypt" tabindex="-1"><a class="header-anchor" href="#bcrypt"><span><code v-pre>bcrypt()</code></span></a></h4>
<p><code v-pre>bcrypt</code> 使用 Bcrypt 对给定的值进行散列。你可以使用它替代 <code v-pre>Hash</code> facade：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">bcrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="broadcast" tabindex="-1"><a class="header-anchor" href="#broadcast"><span><code v-pre>broadcast()</code></span></a></h4>
<p><code v-pre>broadcast</code> 函数将 <a href="https://learnku.com/docs/laravel/5.5/broadcasting" target="_blank" rel="noopener noreferrer">广播</a> 给定的 <a href="https://learnku.com/docs/laravel/5.5/events" target="_blank" rel="noopener noreferrer">事件</a> 到它的监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="blank" tabindex="-1"><a class="header-anchor" href="#blank"><span><code v-pre>blank()</code></span></a></h4>
<p><code v-pre>blank</code> 函数判断给定的值是否为「空」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要使用与 <code v-pre>blank</code> 相反的功能，请看 <a href="https://learnku.com/docs/laravel/5.5/helpers#method-filled" target="_blank" rel="noopener noreferrer">filled</a> 方法。</p>
<h4 id="cache" tabindex="-1"><a class="header-anchor" href="#cache"><span><code v-pre>cache()</code></span></a></h4>
<p><code v-pre>cache</code> 函数可以用来从缓存中获取值。如果缓存中不存在给定的健，则返回一个可选的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将一组键/值对传递给函数来将其添加到缓存中。与此同时，你还应该传递有效的分钟数或持续时间作为缓存过期时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token class-name static-context">Carbon</span><span class="token operator">::</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addSeconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="class-uses-recursive" tabindex="-1"><a class="header-anchor" href="#class-uses-recursive"><span><code v-pre>class_uses_recursive()</code></span></a></h4>
<p><code v-pre>class_uses_recursive</code> 函数返回一个类使用的所有 traits，包括任何子类使用的 traits：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">class_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="collect" tabindex="-1"><a class="header-anchor" href="#collect"><span><code v-pre>collect()</code></span></a></h4>
<p><code v-pre>collect</code> 函数根据给定的数组创建一个 <a href="https://learnku.com/docs/laravel/5.5/collections" target="_blank" rel="noopener noreferrer">集合</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="config" tabindex="-1"><a class="header-anchor" href="#config"><span><code v-pre>config()</code></span></a></h4>
<p><code v-pre>config</code> 函数获取 <a href="https://learnku.com/docs/laravel/5.5/configuration" target="_blank" rel="noopener noreferrer">配置</a> 变量的值。可以使用「点」语法访问配置值，其中包括文件的名称和希望访问的选项。如果配置选项不存在，则可以指定一个默认值并返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以在运行时通过传递一组键/值对来设置配置变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">config</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'app.debug'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="cookie" tabindex="-1"><a class="header-anchor" href="#cookie"><span><code v-pre>cookie()</code></span></a></h4>
<p><code v-pre>cookie</code> 函数创建一个新的 <a href="https://learnku.com/docs/laravel/5.5/requests#cookies" target="_blank" rel="noopener noreferrer">cookie</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-field" tabindex="-1"><a class="header-anchor" href="#csrf-field"><span><code v-pre>csrf_field()</code></span></a></h4>
<p><code v-pre>csrf_field</code> 函数生成包含 CSRF 令牌值的 HTML <code v-pre>hidden</code> 表单字段。例如，使用 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">csrf_field</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-token" tabindex="-1"><a class="header-anchor" href="#csrf-token"><span><code v-pre>csrf_token()</code></span></a></h4>
<p><code v-pre>csrf_token</code> 函数获取当前 CSRF 令牌的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token function">csrf_token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dd" tabindex="-1"><a class="header-anchor" href="#dd"><span><code v-pre>dd()</code></span></a></h4>
<p><code v-pre>dd</code> 函数输出给定的值并结束脚本运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不想终止脚本运行，请改用 <code v-pre>dump</code> 函数。</p>
<h4 id="decrypt" tabindex="-1"><a class="header-anchor" href="#decrypt"><span><code v-pre>decrypt()</code></span></a></h4>
<p><code v-pre>decrypt</code> 函数使用 Laravel 的 <a href="https://learnku.com/docs/laravel/5.5/encryption" target="_blank" rel="noopener noreferrer">加密器</a> 来解密给定的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$decrypted</span> <span class="token operator">=</span> <span class="token function">decrypt</span><span class="token punctuation">(</span><span class="token variable">$encrypted_value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dispatch" tabindex="-1"><a class="header-anchor" href="#dispatch"><span><code v-pre>dispatch()</code></span></a></h4>
<p><code v-pre>dispatch</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/5.5/queues#creating-jobs" target="_blank" rel="noopener noreferrer">任务</a> 推送到 Laravel <a href="https://learnku.com/docs/laravel/5.5/queues" target="_blank" rel="noopener noreferrer">任务列队</a> 中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dispatch-now" tabindex="-1"><a class="header-anchor" href="#dispatch-now"><span><code v-pre>dispatch_now()</code></span></a></h4>
<p><code v-pre>dispatch_now</code> 函数立即运行给定的 <a href="https://learnku.com/docs/laravel/5.5/queues#creating-jobs" target="_blank" rel="noopener noreferrer">任务</a>，并从其 <code v-pre>handle</code> 方法返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">dispatch_now</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dump" tabindex="-1"><a class="header-anchor" href="#dump"><span><code v-pre>dump()</code></span></a></h4>
<p><code v-pre>dump</code> 函数打印给定的变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果要在打印变量后停止执行脚本，请改用 <a href="#method-dd"><code v-pre>dd</code></a> 函数。</p>
<h4 id="encrypt" tabindex="-1"><a class="header-anchor" href="#encrypt"><span><code v-pre>encrypt()</code></span></a></h4>
<p><code v-pre>encrypt</code> 函数使用 Laravel 的 <a href="https://learnku.com/docs/laravel/5.5/encryption" target="_blank" rel="noopener noreferrer">加密器</a> 对给定的值进行加密：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$encrypted</span> <span class="token operator">=</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token variable">$unencrypted_value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="env" tabindex="-1"><a class="header-anchor" href="#env"><span><code v-pre>env()</code></span></a></h4>
<p><code v-pre>env</code> 函数获取 <a href="https://learnku.com/docs/laravel/5.5/configuration#environment-configuration" target="_blank" rel="noopener noreferrer">环境变量</a> 的值或者返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 如果环境变量不存在则返回默认值...</span></span>
<span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="event" tabindex="-1"><a class="header-anchor" href="#event"><span><code v-pre>event()</code></span></a></h4>
<p><code v-pre>event</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/5.5/events" target="_blank" rel="noopener noreferrer">事件</a> 分派给它的监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="factory" tabindex="-1"><a class="header-anchor" href="#factory"><span><code v-pre>factory()</code></span></a></h4>
<p><code v-pre>factory</code> 函数根据给定的类、名称和数量创建一个模型工厂构建器。可以在 <a href="https://learnku.com/docs/laravel/5.5/database-testing#writing-factories" target="_blank" rel="noopener noreferrer">测试</a> or <a href="https://learnku.com/docs/laravel/5.5/seeding#using-model-factories" target="_blank" rel="noopener noreferrer">数据填充</a> 中使用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">factory</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">make</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="filled" tabindex="-1"><a class="header-anchor" href="#filled"><span><code v-pre>filled()</code></span></a></h4>
<p><code v-pre>filled</code> 函数判断给定的值是否不为「空」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要使用与 <code v-pre>filled</code> 相反的功能，请看 <a href="https://learnku.com/docs/laravel/5.5/helpers#method-blank" target="_blank" rel="noopener noreferrer">blank</a> 方法。</p>
<h4 id="info" tabindex="-1"><a class="header-anchor" href="#info"><span><code v-pre>info()</code></span></a></h4>
<p><code v-pre>info</code> 函数将信息写入日志：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Some helpful information!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有前后关系的数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User login attempt failed.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="logger" tabindex="-1"><a class="header-anchor" href="#logger"><span><code v-pre>logger()</code></span></a></h4>
<p><code v-pre>logger</code> 函数可以将一个 <code v-pre>debug</code> 级别的消息写入到 <a href="https://learnku.com/docs/laravel/5.5/errors#logging" target="_blank" rel="noopener noreferrer">日志</a> 中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Debug message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有前后关系的数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User has logged in.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有传值给函数则返回 <a href="https://learnku.com/docs/laravel/5.5/errors#logging" target="_blank" rel="noopener noreferrer">日志</a> 的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'You are not allowed here.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="method-field" tabindex="-1"><a class="header-anchor" href="#method-field"><span><code v-pre>method_field()</code></span></a></h4>
<p><code v-pre>method_field</code> 函数生成一个 HTML <code v-pre>hidden</code> 表单字段，其中包含表单的 HTTP 动作的欺骗值。例如，使用 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>form method<span class="token operator">=</span><span class="token string double-quoted-string">"POST"</span><span class="token operator">></span></span>
<span class="line">    <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">method_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'DELETE'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="now" tabindex="-1"><a class="header-anchor" href="#now"><span><code v-pre>now()</code></span></a></h4>
<p><code v-pre>now</code> 函数为当前时间创建一个新的 <code v-pre>Illuminate\Support\Carbon</code> 实例:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$now</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="old" tabindex="-1"><a class="header-anchor" href="#old"><span><code v-pre>old()</code></span></a></h4>
<p><code v-pre>old</code> 函数 <a href="https://learnku.com/docs/laravel/5.5/requests#retrieving-input" target="_blank" rel="noopener noreferrer">获取</a> 会话中闪存的 <a href="https://learnku.com/docs/laravel/5.5/requests#old-input" target="_blank" rel="noopener noreferrer">旧输入</a> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="optional" tabindex="-1"><a class="header-anchor" href="#optional"><span><code v-pre>optional()</code></span></a></h4>
<p><code v-pre>optional</code> 函数可以接受任何参数，并且允许你访问该对象的属性或者调用方法。如果给定的对象是 <code v-pre>null</code> ， 那么属性和方法会简单地返回 <code v-pre>null</code> 而不是产生一个错误：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">address</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">street</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="policy" tabindex="-1"><a class="header-anchor" href="#policy"><span><code v-pre>policy()</code></span></a></h4>
<p><code v-pre>policy</code> 方法为给定的类获取一个 <a href="https://learnku.com/docs/laravel/5.5/authorization#creating-policies" target="_blank" rel="noopener noreferrer">策略</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$policy</span> <span class="token operator">=</span> <span class="token function">policy</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="redirect" tabindex="-1"><a class="header-anchor" href="#redirect"><span><code v-pre>redirect()</code></span></a></h4>
<p><code v-pre>redirect</code> 函数返回一个 <a href="https://learnku.com/docs/laravel/5.5/responses#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a>，如果没有没有传入参数，则返回重定向实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="report" tabindex="-1"><a class="header-anchor" href="#report"><span><code v-pre>report()</code></span></a></h4>
<p><code v-pre>report</code> 函数将使用 <a href="https://learnku.com/docs/laravel/5.5/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 的 <code v-pre>report</code> 方法抛出异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report</span><span class="token punctuation">(</span><span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="request" tabindex="-1"><a class="header-anchor" href="#request"><span><code v-pre>request()</code></span></a></h4>
<p><code v-pre>request</code> 函数返回当前 <a href="https://learnku.com/docs/laravel/5.5/requests" target="_blank" rel="noopener noreferrer">请求</a> 实例或者获取输入项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$default</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rescue" tabindex="-1"><a class="header-anchor" href="#rescue"><span><code v-pre>rescue()</code></span></a></h4>
<p><code v-pre>rescue</code> 函数执行给定的闭包并捕获执行期间发生的任何异常。所有被捕获的异常将被发送到你的 <a href="https://learnku.com/docs/laravel/5.5/errors#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 的 <code v-pre>report</code> 方法。要注意的是，该请求将继续处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以将第二个参数传递给 <code v-pre>rescue</code> 方法。如果在执行闭包时发生异常，这个参数将是应该返回的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">failure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="resolve" tabindex="-1"><a class="header-anchor" href="#resolve"><span><code v-pre>resolve()</code></span></a></h4>
<p><code v-pre>resolve</code> 函数使用 <a href="https://learnku.com/docs/laravel/5.5/container" target="_blank" rel="noopener noreferrer">服务容器</a> 将给定的类或接口名称解析为其实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="response" tabindex="-1"><a class="header-anchor" href="#response"><span><code v-pre>response()</code></span></a></h4>
<p><code v-pre>response</code> 函数创建 <a href="https://learnku.com/docs/laravel/5.5/responses" target="_blank" rel="noopener noreferrer">响应</a> 实例或者获取响应工厂实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="retry" tabindex="-1"><a class="header-anchor" href="#retry"><span><code v-pre>retry()</code></span></a></h4>
<p><code v-pre>retry</code> 函数尝试执行给定的回调，直到到达给定的最大尝试次数。如果回调没有抛出异常，则返回值将被返回。如果回调抛出异常，它将自动重试。如果超过最大尝试次数，则会抛出异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 在 100ms 左右尝试 5 次...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="session" tabindex="-1"><a class="header-anchor" href="#session"><span><code v-pre>session()</code></span></a></h4>
<p><code v-pre>session</code> 函数可以用来获取或者设置 <a href="https://learnku.com/docs/laravel/5.5/session" target="_blank" rel="noopener noreferrer">Session</a> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过将一组键/值对传递给该函数来设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'chairs'</span> <span class="token operator">=></span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'instruments'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有传递值给函数，则返回 Session 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tap" tabindex="-1"><a class="header-anchor" href="#tap"><span><code v-pre>tap()</code></span></a></h4>
<p><code v-pre>tap</code> 函数接受两个参数：一个任意的 <code v-pre>$value</code> 和一个闭包。<code v-pre>$value</code> 将被传递给闭包，然后由 <code v-pre>tap</code> 函数返回。不需要在闭包中使用 <code v-pre>return</code> 返回值。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有闭包被传递给 <code v-pre>tap</code> 函数，你可以调用给定 <code v-pre>$value</code> 的任何方法。而你调用的方法的返回值始终为 <code v-pre>$value</code> ，无论方法在其定义中实际返回的是什么。例如，Eloquent 的 <code v-pre>update</code> 方法通常会返回一个整数。但是，我们可以强制通过 <code v-pre>tap</code> 函数链式调用 <code v-pre>update</code> 方法来返回模型本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$name</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="today" tabindex="-1"><a class="header-anchor" href="#today"><span><code v-pre>today()</code></span></a></h4>
<p><code v-pre>today</code> 函数为当前日期创建一个新的 <code v-pre>Illuminate\Support\Carbon</code> 实例:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$today</span> <span class="token operator">=</span> <span class="token function">today</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="throw-if" tabindex="-1"><a class="header-anchor" href="#throw-if"><span><code v-pre>throw_if()</code></span></a></h4>
<p>如果给定的布尔表达式计算结果为 <code v-pre>true</code>，<code v-pre>throw_if</code> 函数抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'You are not allowed to access this page'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="throw-unless" tabindex="-1"><a class="header-anchor" href="#throw-unless"><span><code v-pre>throw_unless()</code></span></a></h4>
<p>如果给定的布尔表达式计算结果为<code v-pre>false</code>，则 <code v-pre>throw_unless</code>函数会抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'You are not allowed to access this page'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trait-uses-recursive" tabindex="-1"><a class="header-anchor" href="#trait-uses-recursive"><span><code v-pre>trait_uses_recursive()</code></span></a></h4>
<p><code v-pre>trait_uses_recursive</code> 函数返回一个类使用的所有 trait：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">trait_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="transform" tabindex="-1"><a class="header-anchor" href="#transform"><span><code v-pre>transform()</code></span></a></h4>
<p>如果给定的值不为空，那么 <code v-pre>transform</code> 函数对给定的值执行闭包并返回其结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认值或闭包也可以作为方法的第三个参数传递。如果给定值为空白，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'The value is blank'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The value is blank</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="validator" tabindex="-1"><a class="header-anchor" href="#validator"><span><code v-pre>validator()</code></span></a></h4>
<p><code v-pre>validator</code> 函数用给定的参数创建一个新的 <a href="https://learnku.com/docs/laravel/5.5/validation" target="_blank" rel="noopener noreferrer">验证器</a> 实例。为方便起见，你可以使用它来代替 <code v-pre>Validator</code> facade ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token function">validator</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="value" tabindex="-1"><a class="header-anchor" href="#value"><span><code v-pre>value()</code></span></a></h4>
<p><code v-pre>value</code> 函数返回给定的值。但是，如果将一个闭包传递给该函数，则将执行该闭包并返回其结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="view" tabindex="-1"><a class="header-anchor" href="#view"><span><code v-pre>view()</code></span></a></h4>
<p><code v-pre>view</code> 函数获取一个 <a href="https://learnku.com/docs/laravel/5.5/views" target="_blank" rel="noopener noreferrer">视图</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="with" tabindex="-1"><a class="header-anchor" href="#with"><span><code v-pre>with()</code></span></a></h4>
<p><code v-pre>with</code> 函数会返回给定的值。如果传入一个闭包作为该函数的第二个参数，会返回闭包执行的结果：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token function">is_numeric</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://github.com/medz" target="_blank" rel="noopener noreferrer">Seven Du</a></td>
<td><img src="https://cdn.learnku.com/uploads/images/201910/21/23464/nSIABAcJ8T.jpeg!large" alt="nSIABAcJ8T.jpeg!large"></td>
<td>翻译</td>
<td>基于 Laravel 的社交开源系统 <a href="https://github.com/slimkit/thinksns-plus" target="_blank" rel="noopener noreferrer">ThinkSNS+</a> 欢迎 Star。</td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


